package com.hwl.eduservice.controller.admin;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hwl.commonutils.result.RespBean;
import com.hwl.commonutils.utils.AssertUtil;
import com.hwl.eduservice.pojo.Teacher;
import com.hwl.eduservice.pojo.vo.TeacherQueryVo;
import com.hwl.eduservice.service.TeacherService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author long
 * @since 2021-09-21
 */
@Api(tags = "讲师管理")
@RestController
@RequestMapping("/admin/eduTeacher")
public class TeacherController {

    @Resource
    private TeacherService teacherService;

    @ApiOperation("获取所有讲师")
    @GetMapping("findAll")
    public RespBean getAllTeacher() {
        return RespBean.ok().data(teacherService.list(null));
    }

    @ApiOperation("讲师分页列表")
    @GetMapping("list/{page}/{limit}")
    public RespBean getAllTeacher(@ApiParam(value = "当前页码", required = true) @PathVariable Long page,
                                  @ApiParam(value = "每页大小", required = true) @PathVariable Long limit,
                                  TeacherQueryVo teacherQueryVo) {
        IPage<Teacher> teacherIPage = teacherService.selectPage(new Page<>(page, limit), teacherQueryVo);
        return RespBean.ok().data(teacherIPage);
    }

    @ApiOperation("根据id删除讲师")
    @DeleteMapping("remove/{id}")
    public RespBean removeTeacherById(@PathVariable String id) {
        //TODO 这里是假删除，应该只是禁用功能，不应该去删除阿里云的讲师头像
        //TODO 如果该讲师下还有课程如何处理？
        teacherService.removeAvatarById(id);
        if (teacherService.removeById(id)) {
            return RespBean.ok().message("删除讲师成功！");
        }
        return RespBean.error().message("删除讲师失败！");
    }

    @ApiOperation("添加讲师")
    @PostMapping("save")
    public RespBean saveTeacher(@RequestBody Teacher teacher) {
        //经测试，MP查询的语句会加入逻辑删除判断，而表中的name字段是唯一索引，所以这里不能用MP提供的方法查询讲师名字
        AssertUtil.isTrue(teacherService.getTeaByName(teacher.getName()) != null, "讲师名字不得重复！");
        if (teacherService.save(teacher)) {
            return RespBean.ok().message("添加讲师成功！");
        }
        return RespBean.error().message("添加讲师失败！");
    }

    @ApiOperation("更新讲师信息")
    @PutMapping("update")
    public RespBean updateTeacher(@RequestBody Teacher teacher) {
        Teacher teaByName = teacherService.getTeaByName(teacher.getName());
        AssertUtil.isTrue(teaByName != null && !teaByName.getId().equals(teacher.getId()), "讲师名字不得重复!");

        if (teacherService.updateById(teacher)) {
            return RespBean.ok().message("更新讲师信息成功！");
        }
        return RespBean.error().message("更新讲师信息失败！");
    }

    @ApiOperation("根据id获取讲师信息")
    @GetMapping("getById/{id}")
    public RespBean getById(@PathVariable String id) {
        return RespBean.ok().data(teacherService.getById(id));
    }

    @ApiOperation("根据id列表批量删除讲师")
    @DeleteMapping("deleteByIds")
    public RespBean deleteBatch(@RequestBody List<String> ids) {
        //TODO 讲师头像？？？
        if (teacherService.removeByIds(ids)) {
            return RespBean.ok().message("删除讲师列表成功！");
        }
        return RespBean.error().message("删除讲师列表失败！");
    }

    @ApiOperation("根据关键词查询讲师")
    @GetMapping("query/{keyword}")
    public RespBean queryByKeyword(@ApiParam(value = "关键字", required = true) @PathVariable String keyword) {
        return RespBean.ok().data(teacherService.selectNameList(keyword));
    }

}

